<?php
/**
 * 微信通知自动化任务
 *
whyshop
 */
class whyshopseckill_auto_yuyue {

    //年
    private $year = 0;
    //月
    private $month = 0;
    //yam值
    private $yam = 0;

    /**
     * 构造初始，存入yam，年和月
     * @access public
     * @version 1 Jul 12, 2011
     */
    public function __construct() {
        //上个月的时间
        $tm = time() - 2419200;
        //yam值
        $this->yam = date('Ym', $tm);
        $this->year = date('Y', $tm);
        $this->month = date('m', $tm);
    }
    
    /**
     * 环境下，每分进行数据统计-不然发布出去2017年03月31日16:34:18
     * @access public
     * @version 1 Jul 13, 2011
     */
    public function minute() {
        $pre_minute_enable =true; //app::get('whyshopseckill')->getConf('pre_minute_enable');

        $pre_minute = app::get('whyshopseckill')->getConf('pre_minute');//提前多久提醒
        $pre_minute = $pre_minute ?$pre_minute:15;
        //开启动通知的话，才执行。先默认是开启
        if($pre_minute_enable) {
            $this->execStatistics($pre_minute);
        }
    }

    /**
     * 每小时进行任务
     * @access public
     * @version 1 Jul 12, 2011
     */
    public function hour() {


    }
    
    /**
     * 每天进行任务
     * @access public
     * @version 1 Jul 13, 2011
     */
    public function day() {}
    
    /**
     * 每周进行任务
     * @access public
     * @version 1 Jul 13, 2011
     */
    public function week() {}
    
    /**
     * 每月进行任务
     * @access public
     * @version 1 Jul 13, 2011
     */
    public function month() {}

    /**
     * 月度佣金进行统计
     * @access public
     * @version 1 Jul 12, 2011
     */
    public function execStatistics($pre_minute) {
        //不存在上个月的佣金记录，进行统计
            $this->genStatistics($pre_minute);
    }

    /**
     * 进行数据统计
     * @access private
     * @version 1 Jul 12, 2011
     */
    private function genStatistics($pre_minute) {
        $db = kernel::database();

        //
        $time=time();
        $sec=$pre_minute*60;
        //$sec=1000;
        $sql='select * from sdb_whyshopseckill_yuyue where seckill_starttime-'.$time.'<='.$sec.' and has_notify="false"';//秒杀开始时间-系统当前系统=提前的时间时触发
        //获取上个月的有效订单佣金记录
        file_put_contents(DATA_DIR.'/testseckill.txt',date('Y-m-d H:i:s')."\r\n".var_export($sql,true),FILE_APPEND);
        $arrUop = $db->select($sql);

        //统计数据
        $arrSts = array();
        $objweixinTmpMsg=kernel::single('weixin_templatemsg');
        $tmp_id= app::get('whyshopseckill')->getConf('wx_template_id');
        file_put_contents(DATA_DIR.'/testseckill.txt',"arruop:".date('Y-m-d H:i:s')."\r\n".var_export($arrUop,true)."\r\n",FILE_APPEND);

        foreach ($arrUop as $row) {
            //联盟商的数据累计
            if ($row['open_id']) {
                $data['open_id']=$row['open_id'];


                $tmpl=$objweixinTmpMsg->loadWxTemplateMsg();

               // $jumptourl= app::get('wap')->router()->gen_url(array('app'=>'b2c','ctl'=>'wap_product','act'=>'index','full'=>1,'arg0'=>$row['whyshopseckill_product_id']));到商品页

               // $jumptourl= app::get('wap')->router()->gen_url(array('app'=>'whyshopseckill','ctl'=>'wap_whyshopseckill','act'=>'index','full'=>1));//到秒杀页面

                $jumptourl= kernel::base_url(1).'/index.php/wap/whyshopseckill.html';

                //print_r(111);exit;
                $textTpl='{"touser":"%s","template_id":"'.$tmp_id.'","url":"'.$jumptourl.'","data":{'.implode(',',$tmpl).'}}';
                //print_r($textTpl);exit;

                //file_put_contents(DATA_DIR.'/testseckill.txt',"tmpl:".date('Y-m-d H:i:s')."\r\n".var_export($textTpl,true)."\r\n",FILE_APPEND);

                $first='您预约的秒杀将在'.date('H',$row['seckill_starttime']).':00'.'开始，点击详情，马上参与';


                /*获取会员名*/
                $name=kernel::single('b2c_user_object')->get_member_name(null,$row['member_id']);;
                /*获取会员名end*/
                $addtime=date('Y-m-d H:i:s',$row['add_time']);

                $detail='';
                $remark='';
                //$postData=sp
                $postData=array(sprintf($textTpl, $row['open_id'], $first, $name,$addtime,$addr,$detail,$remark));

                //print_r($postData);exit;
                file_put_contents(DATA_DIR.'/testseckill.txt',"postdata".date('Y-m-d H:i:s')."\r\n".var_export($postData,true)."\r\n",FILE_APPEND);
                $ret=$objweixinTmpMsg->sendTempateMessage($row['open_id'],$postData);
                file_put_contents(DATA_DIR.'/testseckill.txt',"ret".date('Y-m-d H:i:s')."\r\n".var_export($ret,true)."\r\n",FILE_APPEND);

                if($ret['errcode']=='0') {
                    //成功更新has_notify
                    $strSql = 'update sdb_whyshopseckill_yuyue set notify_time = ' . time(). ',has_notify="true",notify_msg_id="'.$ret['msgid'].'",msg=\''.json_encode($ret).'\' where  yuyue_id= ' . $row['yuyue_id'];

                    file_put_contents(DATA_DIR.'/testseckill.txt',"postdata".date('Y-m-d H:i:s')."\r\n".var_export($strSql,true)."\r\n",FILE_APPEND);

                    $db->exec($strSql);
                }
                else{
                    //不成更新msg
                    $strSql = 'update sdb_whyshopseckill_yuyue set notify_time = ' . time(). ',msg=\''.json_encode($ret).'\' where  yuyue_id= ' . $row['yuyue_id'];
                    $db->exec($strSql);

                    file_put_contents(DATA_DIR.'/testseckill.txt',"postdata".date('Y-m-d H:i:s')."\r\n".var_export($strSql,true)."\r\n",FILE_APPEND);

                }
            }
        }


    }
}